package org.oos.model;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.oos.OOS4Java;
import org.oos.atom.elements.OfferElement;
import org.oos.atom.entries.OfferEntry;

public class Offer extends AbstractEntityImpl {

	private static final long serialVersionUID = 4802583412016089150L;
	
	private OfferElement element;
	private OfferEntry entry;

	public Offer(OOS4Java oos4Java, OfferEntry entry) {
		super(oos4Java);
		this.entry = entry;
	}
	
	public Offer(OOS4Java oos4Java, OfferElement element) {
		super(oos4Java);
		this.element = element;
	}

	public String getTitle() {
		String title = null;
		if (entry != null) {
			title = entry.getTitle().getValue();
		} else if(element != null) {
			title = element.getTitle();
		}
		return title;
	}

	public String getSummary() {
		String summary = null;
		if (entry != null) {
			summary = entry.getSummary().getValue();
		} else if(element != null) {
			summary = element.getSummary();
		}
		return summary;
	}

	public String getId() {
		String id = null;
		if (entry != null) {
			id = entry.getId();
		} else if(element != null) {
			id = element.getId();
		}
		return id;
	}

	public String getServiceSlug() {
		String slug = null;
		if (entry != null) {
			slug = entry.getServiceSlug();
		} else if(element != null) {
			
		}
		return slug;
	}

	public Date getValidFrom() {
		Date validFrom = null;
		if (entry != null) {
			validFrom = entry.getValidFrom();
		}
		return validFrom;
	}

	public Date getValidTo() {
		Date validTo = null;
		if(entry != null) {
			validTo = entry.getValidTo();
		}
		return validTo;
	}

	private static Map<String, OfferType> offerTypeMap = new HashMap<String, Offer.OfferType>();
	
	public static enum OfferType {
		Percentage("P"), Bogof("X"), Money("M"), Gift("G");
		
		private OfferType(String key) {
			offerTypeMap.put(key, this);
		}
		
		public static final OfferType offerTypeFrom(String key) {
			return offerTypeMap.get(key);
		}
	}
	
	public OfferType getOfferType() {
		String key = null;
		
		if (entry != null) {
			key = entry.getOfferType();
		} else if(element != null) {
			key = element.getOfferType();
		}
		
		if (key != null) {
			return OfferType.offerTypeFrom(key);
		} else {
			return null;
		}
	}

	public String getDescription() {
		String description = null;
		if (entry != null) {
			description = entry.getDescription();
		}
		return description;
	}

	public String getConditions() {
		String conditions = null;
		if (entry != null) {
			conditions = entry.getConditions();
		}
		return conditions;
	}

	public Integer getSignups() {
		Integer signups = null;
		if (entry != null) {
			signups = entry.getSignups();
		} else if(element != null) {
			signups = element.getSignup();
		}
		return signups;
	}

	public String getValue() {
		String value = null;
		if (entry != null) {
			value = entry.getValue();
		} else if (element != null) {
			value = element.getValue();
		}
		return value;
	}
	
}
